<%@page import="java.text.SimpleDateFormat"%>
<%@page import="classes.Conexao"%>
<%@page import="java.util.Date"%>
<%@page buffer="500kb" autoFlush="false" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" import="java.sql.*"%>
<%
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    
    String str = " ";

    Long cd_usuario = Long.parseLong(session.getAttribute("cd_usuario").toString());
    Long cd_refeicao = Long.parseLong(request.getParameter("cd_refeicao").toString());    
    Long cd_grupo = Long.parseLong(request.getParameter("cd_grupo").toString());    
    java.sql.Date dt_usuario_refeicao = new java.sql.Date(df.parse(df.format(new Date().getTime())).getTime());

    String SQL = "SELECT a.nm_alimento, SUM(ur.qt_porcao), string_agg(ur.cd_usuario_refeicao::text, ','), a.cd_grupo "
            + "FROM \"Operacoes\".\"USUARIO_REFEICAO\" ur, \"Operacoes\".\"ALIMENTO\" a "
            + "WHERE a.cd_alimento = ur.cd_alimento AND ur.cd_refeicao = " + cd_refeicao + " "
            + "AND ur.dt_usuario_refeicao = '" + dt_usuario_refeicao + "' AND ur.cd_usuario = " + cd_usuario + " AND a.cd_grupo = "+cd_grupo+" "
            + "GROUP BY nm_alimento, cd_grupo ";
    
    try {
        for (Object[] reg : Conexao.getQuery(SQL)) {
            String[] cds = reg[2].toString().split(",");
            String toArray = "[";
            for (int i = 0; i < cds.length; i++) {
                toArray += ""+cds[i]+"";
                if (i < cds.length - 1) {
                    toArray += ", ";
                }
            }
            toArray += "]";
            str += "<tr><td class=\"product-name\"> "+reg[0]+"</td><td class=\"product-name\"> "+reg[1]+" </td><td class=\"product-name\"> "
                    + "<a class=\"btn btn-warning btn-xs\" value=\" "+reg[2]+" \" name=\"btnExcluirCRT\" onclick=\"excluirAlimento("+toArray+", "+cd_refeicao+", "+reg[3]+"); \"><i><svg id=\"canvas-for-livicon-57\" height=\"16\" version=\"1.1\" width=\"16\" xmlns=\"http://www.w3.org/2000/svg\" style=\"overflow: hidden; position: relative; left: -0.100006px; top: 0.883301px;\"> "
                    + "<desc> … </desc><defs></defs>"
                    + "<path style=\"opacity: 0;\" fill=\"#ffffff\" stroke=\"none\" d=\"M36.495,5.671L34.205,6.571000000000001C34.205,6.571000000000001,33.299,7.331,32.951,7.1240000000000006C32.571,6.898000000000001,32.864,5.6240000000000006,32.864,5.6240000000000006L33.598,4.686000000000001L32,2.812L34.031,1.8739999999999999C34.031,1.8739999999999999,34.83,0.46899999999999986,35.025,0.46899999999999986C35.458,0.46899999999999986,37.185,1.8739999999999999,37.185,1.8739999999999999L36.193000000000005,0.7959999999999998L37.186,0L40.211,0.936C40.211,0.936,39.348,2.343,39.778999999999996,2.812S40.601,7.125,40.169999999999995,7.125C39.736,7.125,36.928,4.077999999999999,36.928,4.077999999999999L40.04,7.498999999999999L38.396,7.325999999999999L36.71,8.155L33.814,7.263999999999999L36.495,5.671Z\" opacity=\"0\" stroke-width=\"0\" transform=\"matrix(0.5,0,0,0.5,0,0)\"></path>"
                    + "<path fill=\"#ffffff\" stroke=\"none\" d=\"M24.801,8.1H20V4.7C20,4.369,19.73,4.1000000000000005,19.4,4.1000000000000005H10.6C10.269,4.1000000000000005,10,4.369000000000001,10,4.7V8.1H5.2C4.537,8.1,4,8.637,4,9.3V11H26V9.3C26,8.637,25.463,8.1,24.801,8.1ZM12,8.1V6.1H18V8.1H12Z\" transform=\"matrix(0.5,0,0,0.5,0,0)\" stroke-width=\"0\"></path>"
                    + "<path fill=\"#ffffff\" stroke=\"none\" d=\"M8.4,30H21.6C22.926000000000002,30,24,28.926,24,27.6V12H6V27.6C6,28.926,7.075,30,8.4,30ZM10,26H8V14H10V26ZM14,26H12V14H14V26ZM18,26H16V14H18V26ZM22,26H20V14H22V26Z\" stroke-width=\"0\" transform=\"matrix(0.5,0,0,0.5,0,0)\"></path>"
                    + "</svg></i></a></td></tr>" + "$";
            str += reg[1] + "$";
        }
        str = str.substring(0, str.length() - 1);
        out.println(str);
    } catch (Exception ex1) {
        ex1.printStackTrace();
    }

%>